package net.zjitc.redis;

import java.util.Map;
import net.zjitc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/7 16:03
 * @description:
 */
@Component
public class Redis implements RedisCommand{

  @Autowired
  private JedisPool pool;

  @Autowired
  public Redis(JedisPool pool) {
    this.pool=pool;
  }

  @Override
  public void set(String key, String value) {
    Jedis jedis=null;
    try {
      jedis=pool.getResource();
      jedis.set(key,value);
    }finally {
      if (jedis!=null){
        jedis.close();
      }
    }
  }

  @Override
  public void sadd(String key, String... value) {
    Jedis jedis=null;
    try {
      jedis=pool.getResource();
      jedis.sadd(key,value);
    }finally {
      if (jedis!=null){
        jedis.close();
      }
    }
  }

  @Override
  public void hmset(String key, Map<String, String> value) {
    Jedis jedis=null;
    try {
      jedis=pool.getResource();
      jedis.hmset(key,value);
    }finally {
      if (jedis!=null){
        jedis.close();
      }
    }
  }

  @Override
  public void lpush(String key, String... value) {
    Jedis jedis=null;
    try {
      jedis=pool.getResource();
      jedis.lpush(key,value);
    }finally {
      if (jedis!=null){
        jedis.close();
      }
    }
  }
}
